package br.com.sga.modelo.regra;

import br.com.sga.erro.ConexaoException;
import br.com.sga.erro.DaoException;
import br.com.sga.erro.GeralException;
import br.com.sga.modelo.dao.UsuarioDao;
import br.com.sga.modelo.entidade.Usuario;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author kenzo
 */
public class RUsuarioLogin implements Regra {
    private UsuarioDao dao;
    
    public RUsuarioLogin( UsuarioDao dao ) {
        this.dao = dao;
    }

    @Override
    public String executar(HttpServletRequest request) throws GeralException{
        Usuario bean = null;
        String login = request.getParameter("logiin");
        String senha = request.getParameter("senha");
        try {
            //BeanUtils.populate( bean, request.getParameterMap() );
            bean = dao.getByLogin( login );
            if( bean==null || !bean.getSenha().equals(senha) ) {
                request.setAttribute( "msg", "Login ou senha inválida." );
                return "./akese?acao=tusuario_login";
            }
        //} catch (IllegalAccessException | InvocationTargetException e) {
        //    throw new GeralException( e.getCause() );
        } catch ( ConexaoException | DaoException e ) {
            throw new GeralException( e.getCause() );
        }
        request.getSession().setAttribute("USUARIO_LOGADO",bean);
        return "./akese";
    }

}
